﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.SqlClient;
using System.Drawing;
using System.Net.Mail;

using System.Web.Services;
using AjaxControlToolkit;
using System.IO.MemoryMappedFiles;


using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using System.Data.SqlClient;
using System.IO;


public partial class realia_search_property : System.Web.UI.Page
{
    int count = 0;
    string str = "";
    System.Collections.Generic.List<string> initialList = new System.Collections.Generic.List<string>();
    private DataTable GetData(SqlCommand cmd)
    {
        DataTable dt = new DataTable();
        String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
        SqlConnection con = new SqlConnection(strConnString);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch
        {
            return null;
        }
        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        Session.Clear();
       SqlConnection conn15 = null;
            conn15 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn15.Open();
            SqlCommand cmd15 = new SqlCommand("select * from agent_add where ID=(SELECT MAX(ID) FROM agent_add)  ", conn15);
            SqlDataReader dr15 = cmd15.ExecuteReader();
            if (dr15.Read())
            {

                Label119.Text = dr15[2].ToString();
                Label125.Text = dr15[15].ToString();
                Label121.Text = dr15[13].ToString();



                dr15.Close();
            }
            conn15.Close();
            SqlConnection conn16 = null;
            conn16 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn16.Open();
            SqlCommand cmd16 = new SqlCommand("select * from agent_add where ID=(SELECT MAX(ID)-1 FROM agent_add)   ", conn16);
            SqlDataReader dr16 = cmd16.ExecuteReader();
            if (dr16.Read())
            {

                Label122.Text = dr16[2].ToString();
                Label123.Text = dr16[15].ToString();
                Label124.Text = dr16[13].ToString();


                dr16.Close();
            }
            conn16.Close();
            SqlConnection conn = null;
            conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn.Open();
            SqlCommand cmd = new SqlCommand("select * from properties where ID=(SELECT MAX(ID) FROM properties)", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {


                Label38.Text = dr[1].ToString();
                Label37.Text = dr[15].ToString();
                Label39.Text = dr[10].ToString();



                dr.Close();
            }
            conn.Close();
            conn.Close();
            SqlConnection conn2 = null;
            conn2 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn2.Open();
            SqlCommand cmd2 = new SqlCommand("select * from properties where ID=(SELECT MAX(ID)-1 FROM properties)", conn2);
            SqlDataReader dr2 = cmd2.ExecuteReader();
            if (dr2.Read())
            {


                Label40.Text = dr2[15].ToString();
                Label41.Text = dr2[1].ToString();
                Label42.Text = dr2[10].ToString();


                dr2.Close();
            }
            conn2.Close();
            SqlConnection conn3 = null;
            conn3 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn3.Open();
            SqlCommand cmd3 = new SqlCommand("select * from properties where ID=(SELECT MAX(ID)-2 FROM properties)", conn3);
            SqlDataReader dr3 = cmd3.ExecuteReader();
            if (dr3.Read())
            {


                Label43.Text = dr3[15].ToString();
                Label44.Text = dr3[1].ToString();
                Label45.Text = dr3[10].ToString();

                dr3.Close();
            }
            conn3.Close();
            SqlConnection conn4 = null;
            conn4 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
            conn4.Open();
            SqlCommand cmd4 = new SqlCommand("select * from properties where ID=(SELECT MAX(ID)-3 FROM properties) ", conn4);
            SqlDataReader dr4 = cmd4.ExecuteReader();
            if (dr4.Read())
            {


                Label46.Text = dr4[15].ToString();
                Label47.Text = dr4[1].ToString();
                Label48.Text = dr4[10].ToString();
                dr4.Close();
            }
            conn4.Close();
        
    }
    protected void Button1_Click(object sender, EventArgs e)
    {

    }
    protected void Unnamed1_Click(object sender, EventArgs e)
    {

    }
    protected void Unnamed1_Click1(object sender, EventArgs e)
    {
       

        SqlConnection conn = null;
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
        conn.Open();
        SqlCommand scmd = new SqlCommand("select * from properties ", conn);
        SqlDataReader dr = scmd.ExecuteReader();

        while (dr.Read())
        {

            initialList.Add(dr[2].ToString());
            count = count + 1;

        }
        for (int i = 0; i < count; i++)
        {
            string u = "";
            u = initialList[i];
           // Response.Write(u);

        }
        conn.Close();
        dr.Close();

    //    try
    //    {
    //        SqlConnection con4 = null;
    //        con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con4.Open();
         
    //        SqlCommand hm = new SqlCommand("select * from properties where p_id='" + initialList[0] + "'", con4);

    //        SqlDataReader hro = hm.ExecuteReader();
    //        //Response.Write("Initial" + initialList[0]);
    //        Session["initial0"] = initialList[0].ToString();
           
    //        if (hro.Read())
    //        {
    //            string email_id4;
    //            string w;


    //            email_id4 = hro[2].ToString();
    //            w = hro[1].ToString();
    //            //Response.Write(Label1.Text);
    //            Session["p_id"] = initialList[0].ToString();
    //            //Label1.Text = email_id4;
    //            //Label3.Text = w;
    //            //Label2.Text = hro[6].ToString();
    //            //Label4.Text =hro[16].ToString();
    //            //    Label5.Text =hro[10].ToString();
    //            //    Label61.Text =hro[27].ToString();










    //                //if (Request.QueryString["p_id"] != null)
    //                //{

    //                //    string strQuery = "select profile_pic from properties where p_id=@p_id";

    //                //    SqlCommand cmd = new SqlCommand(strQuery);

    //                //    cmd.Parameters.Add("@p_id", SqlDbType.Int).Value

    //                //    = Convert.ToInt32(Request.QueryString["p_id"]);

    //                //    DataTable dt = GetData(cmd);

    //                //    if (dt != null)
    //                //    {

    //                //        Byte[] bytes = (Byte[])dt.Rows[0]["Data"];

    //                //        Response.Buffer = true;

    //                //        Response.Charset = "";

    //                //        Response.Cache.SetCacheability(HttpCacheability.NoCache);

    //                //        Response.ContentType = dt.Rows[0]["ContentType"].ToString();

    //                //        Response.AddHeader("content-disposition", "attachment;filename="

    //                //        + dt.Rows[2]["Name"].ToString());

    //                //        Response.BinaryWrite(bytes);

    //                //        Response.Flush();

    //                //        Response.End();

    //                //    }

    //                //}






















    //              //string  p_id = "M49180480";
    //              //  if (p_id  != null)
    //              //  {
    //              //      Response.Write("asdasd");
    //              //      string strQuery = "select profile_pic from properties where p_id='"+p_id+"'";
    //              //      SqlCommand cmd = new SqlCommand(strQuery);
    //              //      cmd.Parameters.Add("@p_id", SqlDbType.Int).Value = Convert.ToInt32(p_id );
    //              //      DataTable dt = GetData(cmd);
    //              //      if (dt != null)
    //              //      {
    //              //          Byte[] bytes = (Byte[])dt.Rows[56]["Data"];
    //              //          Response.Buffer = true;
    //              //          Response.Charset = "";
    //              //          Response.Cache.SetCacheability(HttpCacheability.NoCache);
    //              //          Response.ContentType = dt.Rows[56]["ContentType"].ToString();
    //              //          Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[2]["p_id"].ToString());
    //              //          Response.BinaryWrite(bytes);
    //              //          Response.Flush();
    //              //          Response.End();
    //              //          Response.Write ("asdassssd");
    //              //      }
    //              //  }

                 




                   
    //                //SqlCommand command = new SqlCommand
    //                //                    ("select profile_pic from properties where p_id='" + initialList[0] + "'", con4);
    //                ////for retrieving the image field in SQL SERVER EXPRESS
    //                ////Database you should first bring
    //                ////that image in DataList or DataTable
    //                ////then add the content to the byte[] array.
    //                ////That's ALL!
    //                //SqlDataAdapter dp = new SqlDataAdapter(command);
    //                //DataSet ds = new DataSet("MyImages");

    //                //byte[] MyData = new byte[0];

    //                //dp.Fill(ds, "MyImages");
    //                //DataRow myRow;
    //                //myRow = ds.Tables["MyImages"].Rows[0];

    //                //MyData = (byte[])myRow["fldPic"];

    //                //MemoryStream stream = new MemoryStream(MyData);
    //                ////With the code below, you are in fact converting the byte array of image
    //                ////to the real image.
                   
    //                //Image1.ImageUrl = Image.
    //             //string str;

    //             //str = Session["initial0"].ToString();

    //    //imge1.ImageUrl = "search-imagehandler.ashx?p_id=" + str.ToString();

    //   // Response.Redirect("profile.aspx?img=" + Label1.Text.Trim());
    //               // Response.Redirect("Additional.aspx?p_id="+ initialList[0].ToString () );

                    

    //                //SqlCommand command1 = new SqlCommand("select profile_pic from properties where p_id=@param", con4);
    //                //SqlParameter myparam = command1.Parameters.Add("@param", SqlDbType.NVarChar, 30);
    //                //myparam.Value = initialList[0];
    //                //byte[] img = (byte[])command1.ExecuteScalar();
    //                //MemoryStream str = new MemoryStream();
    //                //str.Write(img, 0, img.Length);
    //                //Bitmap bit = new Bitmap(str);

    //                //SqlDataReader dr1 = null;
    //                //SqlConnection con0 = null;
    //                //SqlCommand cmd = null;
                   
    //                //try
    //                //{
    //                //    String str = Label1.Text;
                     
    //                //    int i = 0;

    //                //    con0 = new SqlConnection(System.Configuration.ConfigurationManager.
    //                //         ConnectionStrings["ConnStr"].ConnectionString);

    //                //    cmd = new SqlCommand("select profile_pic from properties where p_id=@p_id", con0);
    //                //    cmd.Parameters.AddWithValue("@p_id", "" + str + "");
    //                //    con0.Open();
    //                //    dr1 = cmd.ExecuteReader();
    //                //    while (dr.Read())
    //                //    {//context .Response .Write (context.Request["initial0"]);

    //                //     Response.ContentType = "image/jpg";
    //                //    Response.BinaryWrite((byte[])dr["profile_pic"]);

    //                //    }
    //                //    if (dr != null)
    //                //        dr.Close();
    //                //}

    //                //finally
    //                //{
    //                //    if (con0 != null)
    //                //        con0.Close();
    //                //}



    //}

            

    //        con4.Close();
    //        hro.Close();
    //    }
    //       // Response .Redirect ("search-property.aspx?initialList[0]=");

    //    catch
    //    {
            
    //        SqlConnection con4 = null;
    //        con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con4.Open();
    //        SqlCommand hmd4 = new SqlCommand("select * from properties where ID=(SELECT MAX(ID) FROM properties)", con4);

    //        SqlDataReader hr4 = hmd4.ExecuteReader();
       
    //        //Response.Write(ddl.Items[0].Value);
    //        if (hr4.Read())
    //        {
    //            string email_id4;
    //            string w3;

    //            email_id4 = hr4[2].ToString();
    //            w3 = hr4[1].ToString();
    //            Session["lable1"] = Label1.Text;
    //            string p_id = Label1.Text;
            
    //            Label1.Text = email_id4;
    //            Label3.Text = w3;
    //            Label2.Text = hr4[6].ToString();
    //            Label4.Text =hr4[16].ToString();
    //                Label5.Text =hr4[10].ToString();
    //                Label61.Text =hr4[27].ToString();



    //        }
    //        hr4.Close();
    //        con4.Close();
    //    }

    //   // Response.Redirect("search-property.aspx? + 




    //    try
    //    {
    //        SqlConnection con5 = null;
    //        con5 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con5.Open();
    //        SqlCommand hm5 = new SqlCommand("select * from properties where p_id='" + initialList[1] + "'", con5);
    //        Session["initial1"] = initialList[1].ToString();
    //        SqlDataReader hro5 = hm5.ExecuteReader();
    //     //   Response.Write("Initial1" + initialList[1]);
           
    //        if (hro5.Read())
    //        {
    //            string email_id5;
    //            string w5;


    //            email_id5 = hro5[2].ToString();
    //            w5= hro5[1].ToString();


    //            Label7.Text = email_id5;
    //            Label9.Text = w5;
    //            Label8.Text = hro5[6].ToString();
    //            Label10.Text =hro5[16].ToString();
    //                Label11.Text =hro5[10].ToString();
    //                Label12.Text =hro5[27].ToString();


    //        }

    //        con5.Close();
    //        hro5.Close();
    //    }

    //    catch
    //    {

    //        SqlConnection con6 = null;
    //        con6 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con6.Open();
    //        SqlCommand hmd6 = new SqlCommand("select * from properties where ID=(SELECT MIN(ID) FROM properties)", con6);

    //        SqlDataReader hr6= hmd6.ExecuteReader();

    //        //Response.Write(ddl.Items[0].Value);
    //        if (hr6.Read())
    //        {
    //            string email_id6;
    //            string w6;

    //            email_id6 = hr6[2].ToString();
    //            w6 = hr6[1].ToString();


    //            Label7.Text = email_id6;
    //            Label9.Text = w6;
    //            Label8.Text = hr6[6].ToString();
    //              Label10.Text =hr6[16].ToString();
    //                Label11.Text =hr6[10].ToString();
    //                Label12.Text =hr6[27].ToString();


    //        }
    //        hr6.Close();
    //        con6.Close();
    //    }





    //    try
    //    {
    //        SqlConnection con51 = null;
    //        con51 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con51.Open();
    //        SqlCommand hm51 = new SqlCommand("select * from properties where p_id='" + initialList[2] + "'", con51);

    //        SqlDataReader hro51 = hm51.ExecuteReader();
    //     //   Response.Write("Initial2" + initialList[2]);
    //        Session["initial2"] = initialList[2].ToString();
    //        if (hro51.Read())
    //        {
    //            string email_id51;
    //            string w51;


    //            email_id51 = hro51[2].ToString();
    //            w51 = hro51[1].ToString();


    //            Label131.Text = email_id51;
    //            Label151.Text = w51;
    //            Label141.Text = hro51[6].ToString();
    //            Label611.Text =hro51[16].ToString();
    //                Label13.Text =hro51[10].ToString();
    //                Label14.Text =hro51[27].ToString();

    //        }

    //        con51.Close();
    //        hro51.Close();
    //    }

    //    catch
    //    {

    //        SqlConnection con61 = null;
    //        con61 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con61.Open();
    //        SqlCommand hmd61 = new SqlCommand("select * from properties where ID=(SELECT MIN(ID)+1 FROM properties)", con61);

    //        SqlDataReader hr61 = hmd61.ExecuteReader();
         
    //        //Response.Write(ddl.Items[0].Value);
    //        if (hr61.Read())
    //        {
    //            string email_id61;
    //            string w61;

    //            email_id61 = hr61[2].ToString();
    //            w61 = hr61[1].ToString();


    //            Label131.Text = email_id61;
    //            Label151.Text = w61;
    //            Label141.Text = hr61[6].ToString();

    //            Label611.Text =hr61[16].ToString();
    //                Label13.Text =hr61[10].ToString();
    //                Label14.Text =hr61[27].ToString();
    //        }
    //        hr61.Close();
    //        con61.Close();
    //    }






    //    try
    //    {
    //        SqlConnection con511 = null;
    //        con511 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con511.Open();
    //        SqlCommand hm511 = new SqlCommand("select * from properties where p_id='" + initialList[3] + "'", con511);

    //        SqlDataReader hro511 = hm511.ExecuteReader();
    //        //Response.Write("Initial3" + initialList[3]);
    //        Session["initial3"] = initialList[3].ToString();
    //        if (hro511.Read())
    //        {
    //            string email_id511;
    //            string w511;


    //            email_id511 = hro511[2].ToString();
    //            w511 = hro511[1].ToString();


    //            Label15.Text = email_id511;
    //            Label17.Text = w511;
    //            Label16.Text = hro511[6].ToString();
    //            Label18.Text =hro511[16].ToString();
    //                Label19.Text =hro511[10].ToString();
    //                Label20.Text =hro511[27].ToString();

    //        }

    //        con511.Close();
    //        hro511.Close();
    //    }

    //    catch
    //    {

    //        SqlConnection con61 = null;
    //        con61 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //        con61.Open();
    //        SqlCommand hmd61 = new SqlCommand("select * from properties where ID=(SELECT MIN(ID)+2 FROM properties)", con61);

    //        SqlDataReader hr61 = hmd61.ExecuteReader();

         
    //        if (hr61.Read())
    //        {
    //            string email_id61;
    //            string w61;

    //            email_id61 = hr61[2].ToString();
    //            w61 = hr61[1].ToString();


    //            //Label15.Text = email_id61;
    //            //Label17.Text = w61;
    //            //Label16.Text = hr61[6].ToString();
    //            //  Label18.Text =hr61[16].ToString();
    //            //    Label19.Text =hr61[10].ToString();
    //            //    Label20.Text = hr61[27].ToString();

    //        }
    //        hr61.Close();
    //        con61.Close();
    //    }
    //    //    SqlConnection con4 = null;
    //    //    con4 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //    //    con4.Open();
    //    //    SqlCommand hmd4 = new SqlCommand("select * from properties where p_id='" + initialList[1] + "'", con4);

    //    //    SqlDataReader hr4 = hmd4.ExecuteReader();

    //    //    //Response.Write(ddl.Items[0].Value);
    //    //    if (hr4.Read())
    //    //    {
    //    //        string email_id4;


    //    //        email_id4 = hr4[2].ToString();



    //    //        Label1.Text = email_id4;




    //    //    }
    //    //    hr4.Close();
    //    //    con4.Close();
    //    //    SqlConnection con41 = null;
    //    //    con41 = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
    //    //    con41.Open();
    //    //    SqlCommand hmd41 = new SqlCommand("select * from properties where p_id='" + initialList[1] + "'", con41);

    //    //    SqlDataReader hr41 = hmd41.ExecuteReader();

    //    //    //Response.Write(ddl.Items[0].Value);
    //    //    if (hr41.Read())
    //    //    {
    //    //        string email_id41;


    //    //        email_id41 = hr41[2].ToString();



    //    //        Label7.Text = email_id41;




    //    //    }
    //    //    hr41.Close();
    //    //    con41.Close();
    //    //}
    }
}
